<?php
/**
 *  
 *
  */
namespace Org\Util;
class Jsjnd {

    
     public function balance() {
		$condition['c_id']=15;
		$condition['status']=0;
 		$order=M('lottery_order')->where($condition)->select();  //所有没有结算订单
		
		$period=M('lottery_order')->distinct(true)->field('c_no')->where($condition)->select();  //没结算期数
		foreach($period as $k=>$v){
 		  $condition1['c_id']=15;
		  $condition1['c_no']=$v['c_no'];
		  $cqssc_result[$v['c_no']]=M('lottery_results')->where($condition1)->getField('lottery_result');  //根据期数获取开奖结果
 		}
		
		foreach($order as $ok=>$ov){
 		   if($cqssc_result[$ov['c_no']]){    //有开奖结果才结算
 		      $this->jiesuan($cqssc_result[$ov['c_no']],$ov);
			  }
 		}
		return; 		
     }
	
	 public function jiesuan($result,$info=array())
	 {
		 
		 $num= explode(",",$result);
		 $sum=array_sum($num);
 		 
		 //总和大小玩法 ZDX
		 if($info['w_type']=='ZDX'){
 			 $this->zh_dx($sum,$info);
			 }

		 //总和大小玩法 ZHT 和值
		 if($info['w_type']=='ZHT'){
 			 $this->zh_zht($sum,$info);
			 }

		 //总和单双玩法 ZDS
		 if($info['w_type']=='ZDS'){
			 $this->zh_ds($sum,$info);
			 }
		  
         //总和 DXDS
		 if($info['w_type']=='DXDS'){
			 $this->zh_dxds($sum,$info);
			 }
        //前后和 QHH
		 if($info['w_type']=='QHH'){
			 $this->zh_qh($num,$info);
			 }
		//单双和 DSH
		 if($info['w_type']=='DSH'){
			 $this->zh_dsh($num,$info);
			 }	 
		//单双和 ZMZM
		 if($info['w_type']=='ZM'){
			 $this->zh_zm($num,$info);
			 }
			  //五行
		 if($info['w_type']=='WX'){  
			 $this->zh_wx($sum,$info);
			 }
		 
 		 }
		 
		 public function zh_wx($sum,$info){
	
	   if($sum<=695){
		  $dx='金';
		  }elseif($sum<=763&&$sum>=696){
			$dx='木';  
			  }elseif($sum<=855&&$sum>=764){
			$dx='水';  
			  }elseif($sum<=923&&$sum>=856){
			$dx='火';  
			  }elseif($sum<=1410&&$sum>=924){
			$dx='土';  
			  }

	 if($dx==$info['t_name']){  //中奖
				  $this->zhongjiang($info);
	 			  }else{
				  //不中奖  判断是否反水
	 			     $this->fanshui($info);
	 			 }
	  }
	public function zh_dx($sum,$info){  //总和大小
	  
            if($sum==810){
			  $this->benjin($info); //当和值等于810,则大小退回本金
			}else{  
			  if($sum>810){
			     $dx='D';	 
				}else{
				 $dx='X';	 
				}
	 	      if($dx==$info['t_type']){  //中奖
				  $this->zhongjiang($info);
	 			  }else{
				  //不中奖  判断是否反水
	 			     $this->fanshui($info);
	 			 }
			 
			}
       }
    public function zh_zht($sum,$info){
    	if($sum==810){
    	    $this->zhongjiang($info);
	 		}else{
			//不中奖  判断是否反水
	 		 $this->fanshui($info);
	 		}


    }

	public function zh_ds($sum,$info){  //总和单双
      if($sum%2==0){
		$ds='S';
			}else{
		$ds='D';	 
		}
	 if($ds==$info['t_type']){  //中奖
			  $this->zhongjiang($info);
 			  }else{
			  //不中奖  判断是否反水
 					  $this->fanshui($info);
 			 }
    }


    public function zh_dxds($sum,$info){
	 if($sum%2==0&&$sum>810){
		$dx='DS'; 
		 }elseif($sum%2==0&&$sum<810){
		$dx='XS'; 	 
			 }elseif($sum%2!=0&&$sum<810){
		$dx='XD'; 	 
			 }elseif($sum%2!=0&&$sum>810){
		$dx='DD'; 	 
			 }else{
		$dx='T';		 
				 }
	    if($dx==$info['t_type']){
    	    $this->zhongjiang($info);
	 		}else{
			//不中奖  判断是否反水
	 		 $this->fanshui($info);
	 		}
	 		 
	}


	public function zh_qh($num,$info){
	  $a=1;
	  $b=1;
	  foreach($num as $k=>$v){
		  if($v<=40){
			  $a=$a+1;
			  }else{
				$b=$b+1;  
				  }
		  }
	    if($a>$b){
		  $dx='Q';
		  }elseif($a<$b){
			$dx='H';  
			  }else{
			$dx='T';  	  
				  }	 

	   if($dx==$info['t_type']){
    	    $this->zhongjiang($info);
	 		}else{
			//不中奖  判断是否反水
	 		 $this->fanshui($info);
	 		}
	}


	public function zh_dsh($num,$info){
	  $a=1;
	  $b=1;
	    foreach($num as $k=>$v){
		  if($v%2==0){
			  $a=$a+1;
			  }else{
				$b=$b+1;  
				  }
		}
	    if($a>$b){
		  $dx='S';
		  }elseif($a<$b){
			$dx='D';  
			  }else{
			$dx='T';  	  
		}	  
	    if($dx==$info['t_type']){
    	    $this->zhongjiang($info);
	 		}else{
			//不中奖  判断是否反水
	 		 $this->fanshui($info);
	 	}
	}

	public function wx($sum){
	  
	    if($sum<=695){
		  $dx=0;
		  }elseif($sum<=763&&$sum>=696){
			$dx=1;  
			  }elseif($sum<=855&&$sum>=764){
			$dx=2;  
			  }elseif($sum<=923&&$sum>=856){
			$dx=3;  
			  }elseif($sum<=1410&&$sum>=924){
			$dx=4;  
		}
		if($dx==$info['t_type']){
    	    $this->zhongjiang($info);
	 		}else{
			//不中奖  判断是否反水
	 		 $this->fanshui($info);
	 	}
	   
	}

	public function zh_zm($num,$info){
		if (in_array($info['t_type'], $num)){
			$this->zhongjiang($info);
			}else{
			//不中奖  判断是否反水
 			$this->fanshui($info);
		}

	}	
	
 
  
 
   
 public function zhongjiang($info){
 	$money=round((float)$info['money']*$info['odds'],2);
	//print_r($money);exit;
	$updata_data['is_winning'] = 1;
	$updata_data['status'] = 1;
	$updata_data['js_time'] = time();
	$updata_data['win_money'] =$money;
	$updata_data['win_money'] =round((float)$info['money']*($info['odds']-1),2);
	M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
	M('member')->where('member_id='.$info['m_id'])->setInc('member_money',$money); // 用户的余额增加
	//添加余额变动记录
	$insert_log['m_id']=$info['m_id'];
	$insert_log['change_money']=$money;
	$insert_log['change_type']=5;
	$insert_log['add_time']=time();
	$insert_log['remark']='加拿大快8中奖 下注期号:'.$info['c_no'].'订单号:'.$info['order_sn'];
	M('account_log')->add($insert_log);
	 }

 public function benjin($info){
 	$money=$info['money'];
	//print_r($money);exit;
	$updata_data['is_winning'] = 3;
	$updata_data['status'] = 1;
	$updata_data['js_time'] = time();
	M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
	M('member')->where('member_id='.$info['m_id'])->setInc('member_money',$money); // 用户的余额增加
	//添加余额变动记录
	$insert_log['m_id']=$info['m_id'];
	$insert_log['change_money']=$money;
	$insert_log['change_type']=5;
	$insert_log['add_time']=time();
	$insert_log['remark']='加拿大快8退本金 下注期号:'.$info['c_no'].'订单号:'.$info['order_sn'];
	M('account_log')->add($insert_log);
	 }	 
	 
 public function fanshui($info){
 	  $updata_data['is_winning'] = 2;
	  $updata_data['status'] = 1;
	  $updata_data['js_time'] = time();
	 // M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
		if($info['rebate']>0){
			M('member')->where('member_id='.$info['m_id'])->setInc('member_money',$info['rebate']); // 用户的余额增加
			//添加余额变动记录
			$insert_log['m_id']=$info['m_id'];
			$insert_log['change_money']=$info['rebate'];
			$insert_log['change_type']=2;
			$insert_log['add_time']=time();
			$insert_log['remark']='加拿大快8反水 下注期号:'.$info['c_no'].'订单号:'.$info['order_sn'];
			M('account_log')->add($insert_log);
		}
			 $updata_data['lost_money'] =$info['money']-$info['rebate'];
		  M('lottery_order')->where('order_id='.$info['order_id'])->save($updata_data); // xi
	 }	 
	 
}